<template>
  <div class="layout-col flex1 bss">
    <div class="ww100 flex-bc plr12 ptb8 bs4">
      <div class="flex-sc">
        <span class="f15 fw">变位告警 - {{ isNull(publicStore.active)?'':publicStore.active.name }}</span>
      </div>
      <div class="flex-ec flex1 h30">
        <div class="rad4 ptb6 plr10 flex-cc cursor bgi3 white ml10" 
          v-if="state.content.find(v=>{return typeof(v.key) == 'object'}) && state.content.find(v=>{return typeof(v.key) == 'object'})['key'].hasOwnProperty('add')" 
          @click.stop="handleClick('add', state.addItem)">
          <i-ep-plus class="f12 fw" /><span class="f14 ml5">{{state.content.find(v=>{return typeof(v.key) == 'object'})['key'].add}}</span>
        </div>
      </div>
    </div>
    <div class="row bgi22">
      <div :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i" v-show="v.show" :class="typeof(v.key) == 'object'?v.width +' '+ 'mlr5':v.width">{{v.name}}</div>
    </div>
    <div class="table">
      <div class="row" :class="publicStore.active[state.key] == item[state.key]?'active':index%2 == 1?'bgi23':'bgi18'" v-for="(item, index) in state.list" :key="index" @click="handleClick('active', item)">
        <div v-show="v.show" :class="v.width" :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i">
          <span v-if="typeof(v.key) == 'object'" class="i21 cursor mlr5" :class="kk=='add'?'none':''" v-for="(vv, kk, ii) in v.key" :key="ii" @click.stop="handleClick(kk, item)">{{vv}}</span>
           <span v-else-if="v.key=='*'">{{index+1}}</span>
          <span v-else-if="v.type == 'time'">{{item[v.key] == 0?'/':parseTime(item[v.key])}}</span>
          <span v-else-if="v.type == 'find'" :class="find(v.value[0], [v.value[1], item[v.key]], 'color')?find(v.value[0], [v.value[1], item[v.key]], 'color'):''">
            {{find(v.value[0], [v.value[1], item[v.key]], v.value[2])}}
          </span>
          <span v-else-if="v.type == 'switch'">
            <el-switch v-model="item[v.key]" :active-text="v.value[1].name" :inactive-text="v.value[0].name" :active-value="v.value[1].value" :inactive-value="v.value[0].value" inline-prompt @change="handleClick('switch', item)"></el-switch>
          </span>
          <span v-else-if="v.type == 'input'"><el-input v-model="item[v.value]" placeholder="请输入" style="width: 150px;" size="small" /></span>
          <span v-else>{{isNull(item[v.key]) && v.key!=''? '/' : item[v.key]}}</span>
        </div>
      </div>
      <div v-if="state.empty" class="flex-cc ww100 tc ptb13 bgi18">没有数据</div>
    </div>
    <Add @init="init" :state="state" ref="exposeRef" />
    <Conn @init="init" :state="state" ref="connRef" />
  </div>
</template>

<script lang="ts" setup>
  import Add from '@/components/Add'
  import Conn from '@/components/Conn'
	const { proxy }:any = getCurrentInstance()
  const publicStore = proxy.publicStore()
  const configStore = proxy.configStore()
  let statisticsType = $ref([
    {value: '0', name: '累计'},
    {value: '1', name: '单次'},
  ])
  let leftRangeType = $ref([
    {value: '0', name: '开区间'},
    {value: '1', name: '闭区间'},
  ])
  let rightRangeType = $ref([
    {value: '0', name: '开区间'},
    {value: '1', name: '闭区间'},
  ])
  const state = reactive({
	  ...publicStore.$state,
    key: 'id',
    content: [
      { width: 'w50x3', show: true, align: 'left', key: 'alarm_code', name: '告警类别ID' },
      { width: 'w50x3', show: true, align: 'left', key: 'alarm_name', name: '警情名称' }, 
      { width: 'w50x4', show: true, align: 'left', key: 'alarm_description', name: '警情描述' }, 
      { width: 'w50x3', show: true, align: 'center', key: 'statistics_type', name: '统计类型', type: 'find', value: [statisticsType, 'value', 'name'] }, 
      { width: 'flex1', show: true, align: 'right', key: {add: '新增',  conn:'联动', edit:'编辑', del: '删除'},  name: '操作' },
    ],
    editFrom: [
      { required: true,  editshow: true, name: '告警类别ID',   key: 'alarm_code', type: ['input'] }, 
      { required: true,  editshow: true, name: '警情名称',   key: 'alarm_name', type: ['input'] }, 
      { required: false, editshow: true, name: '警情描述',   key: 'alarm_description', type: ['input'] }, 
      { required: false, editshow: true, name: '统计类型',   key: 'statistics_type', type: ['select', statisticsType, 'value', 'name'] }, 
    ],
    addItem: {},
  })
  let exposeRef = $ref()
  let connRef = $ref()

  watch(() => publicStore.active, async(val) => {
    await nextTick()
    if(!proxy.isNull(val.children)) return
    if(proxy.isNull(val)) return
    init()
  }, {immediate: false, deep: true})

  const init = async() => {
    state.addItem = {sensor_id: publicStore.active.parent_id, attribute_name: publicStore.active.code}
    state.query = {"o": "o_sensor_alert_state", "p":[publicStore.active.parent_id, publicStore.active.code], "i": 0}
    state.params = {queryApi: state.query}
    let res = await publicStore.post(state.params)
    state.list = proxy.isNull(res)? [] : res
    state.empty = proxy.isNull(res)? true : false
  }

  const handleClick = (remark, val) => {
    if(remark == 'add' || remark == 'edit'){
      exposeRef.onVisable(val)
    }
    if(remark == 'del'){
      exposeRef.del(val, state.query, state.key)
    }
    if(remark == 'conn'){
      connRef.onVisable(val, state.query.o)
    }
  }

</script>
  
<style scoped lang="scss">

</style>
  